<?php
/* SVN FILE: $Id: delete.php 127 2009-07-22 03:55:08Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket ACP home page
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 127 $
* @lastrevision $Date: 2009-07-22 03:55:08 +0000 (Wed, 22 Jul 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-07-22 03:55:08 +0000 (Wed, 22 Jul 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/categories/delete.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_categories_delete extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->html = $this->registry->output->loadTemplate('cp_skin_categories');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_categories' ) );
		
		if(!is_array($this->caches['market_category'][$this->request['category']]))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=categories&amp;section=manage" );
		}
		if(count($this->caches['market_category']) == 1)
		{
			$this->registry->output->showError( $this->lang->words['cannot_remove_last_category'] );
		}
		
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'category_delete' );
		
		if($this->request['request_method'] == 'get')
		{
			$this->registry->output->html = $this->html->deleteCategory($this->caches['market_category'][$this->request['category']]);
			
			$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
			$this->registry->getClass('output')->sendOutput();
		}
		else
		{
			if(!is_null(marketRegistry::getCategoryTree($this->request['category'])))
			{
				if(!is_array($this->caches['market_category'][$this->request['new_parent_cat']]))
				{
					$this->registry->output->showError( $this->lang->words['not_category'] );
				}
				$parent = $this->request['new_parent_cat'];
				while($parent != -1)
				{
					if($parent == $this->request['category'])
					{
						// Or it will create an infinite loop from parent to child
						$this->registry->output->showError( $this->lang->words['cannot_put_child_as_parent'] );
					}
					$parent = $this->caches['market_category'][$parent]['cat_parent_id'];
				}
				
				$this->DB->update('market_category',array('cat_parent_id' => $this->request['new_parent_cat']),"cat_parent_id=".$this->request['category']);
			}
			
			if(!is_null(marketRegistry::getItemsByCategory($this->request['category'])))
			{
				if(!is_array($this->caches['market_category'][$this->request['new_item_cat']]) || $this->request['new_item_cat'] == $this->request['category'])
				{
					$this->registry->output->showError( $this->lang->words['not_category'] );
				}
				
				$this->DB->update('market_items',array('category' => $this->request['new_item_cat']),"category=".$this->request['category']);
				
				$this->cache->rebuildCache('market_items','ibmarket');
			}
			
			$this->DB->delete('market_categories',"cat_id=".$this->request['category']);
			$this->cache->rebuildCache('market_category','ibmarket');
			
			$this->lang->words['category_delete_log'] = str_replace( "<#NAME#>", $this->caches['market_category'][$this->request['category']]['cat_name'], $this->lang->words['category_delete_log'] );
			$this->lang->words['category_delete_log'] = str_replace( "<#ID#>", $this->request['category'], $this->lang->words['category_delete_log'] );
			ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['category_delete_log'] );
			
			$this->registry->output->global_message = $this->lang->words['category_deleted'];
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=categories&amp;section=manage&amp;category=".$this->caches['market_category'][$this->request['category']]['cat_parent_id'] );
		}
	}
}